package leetcode.剑指offer.剑指II;

import leetcode.剑指offer.pojo.TreeNode;
import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/3/22 16:45
 * @description：https://leetcode-cn.com/problems/jC7MId/
 */
public class 剑指_II_51_节点之和最大的路径 {
    @Test
    public void test() {
        Solution solution = new Solution();
    }


    class Solution {
        int res = Integer.MIN_VALUE;
        public int maxPathSum(TreeNode root) {
            dfs(root);
            return res;
        }

        private int dfs(TreeNode node) {
            if (node == null) {
                return 0;
            }
            int maxLeft = Math.max(0, dfs(node.left));
            int maxRight = Math.max(0, dfs(node.right));
            res = Math.max(res, node.val + maxLeft + maxRight);
            return node.val + Math.max(maxLeft, maxRight);
        }

    }
}
